package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Strings;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.ArticulusReport;
import com.jrtc.base.entity.vo.ArticulusReportVo;
import com.jrtc.service.ArticulusReportService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 *  关节活动度报告(ArticulusReport)表控制层
 *
 * @author makejava
 * @since 2022-09-07 15:52:18
 */
@RestController
@RequestMapping("articulusReport")
public class ArticulusReportController {
    /**
     * 服务对象
     */
    @Resource
    private ArticulusReportService articulusReportService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ArticulusReportVo selectOne(Integer id) {
        return this.articulusReportService.queryById(id);
    }
    
    @RequestMapping("getArticulusReportList")
    public ResultDTO getArticulusReportList(@RequestBody ArticulusReport articulusReport)
    {
        IPage<ArticulusReport> ipage = articulusReportService.queryArticulusReportList(articulusReport);
        return ResultDTOBuilder.success(ipage);
    }

    /**
     * 添加患者关节活动度记录
     * */
    @RequestMapping("insertArticulusReport")
    public ResultDTO insertArticulusReport(@RequestBody ArticulusReportVo articulusReport) {
        int i;
        try {
            i = articulusReportService.insert(articulusReport);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }

        if (i > 0) {
           return ResultDTOBuilder.success("添加成功！");
        }
        else {
            return ResultDTOBuilder.failureMsg("添加失败！");
        }
    }

    @RequestMapping("getArticulusReportById")
    public ResultDTO getArticulusReportById(@RequestBody Map<String, String> map)
    {
        String articulusReportId = map.get("articulusReportId");
        ArticulusReportVo articulusReport = articulusReportService.queryById(Integer.valueOf(articulusReportId));
        if (Strings.isNullOrEmpty(articulusReportId) || articulusReport==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(articulusReport);
    }

    @RequestMapping("updateArticulusReport")
    public ResultDTO updateArticulusReport(@RequestBody ArticulusReport articulusReport)
    {
        if (articulusReport==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (articulusReportService.update(articulusReport)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteArticulusReport")
    public ResultDTO deleteArticulusReport(@RequestBody Map<String,String> map)
    {
        String articulusReportId = map.get("articulusReportId");
        if (!Strings.isNullOrEmpty(articulusReportId))
        {
            if (articulusReportService.deleteById(Integer.valueOf(articulusReportId)))
            {
                return ResultDTOBuilder.success("删除成功！");
            }
            return ResultDTOBuilder.failureMsg("删除失败！");
        }
        return ResultDTOBuilder.failureMsg("编号不正确！");
    }

    /**
     * 关节活动度图表
     * */
    @PostMapping("/articulusReport")
    public ResultDTO articulusReport(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        ArticulusReportVo articulusReportVo = articulusReportService.articulusReport(id);
        return ResultDTOBuilder.success(articulusReportVo);
    }
}